package com.jsonal.controller.api;

import com.jsonal.common.VerifyCodeUtils;
import com.jsonal.controller.BaseController;
import com.jsonal.entity.api.UserBean;
import com.jsonal.entity.manager.LoginBean;
import com.jsonal.form.manager.LoginForm;
import com.jsonal.form.user.UserForm;
import com.jsonal.service.api.UserService;
import com.jsonal.service.manager.LoginService;
import com.jsonal.util.RedisUtil;
import com.jsonal.vo.Result;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author: jingji.lin
 * @Description:
 * @Date: 2020/4/20 14:40
 * @Version: 1.0
 */
@Controller
@RequestMapping(value = "/api")
@CrossOrigin
@Slf4j
public class LoginController extends BaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private LoginService loginService;

    @RequestMapping(value = "/tokenIsExists/{token}", method = RequestMethod.POST)
    @ResponseBody
    public Result tokenIsExists(@PathVariable("token") String token) {
        boolean key = redisUtil.hasKey(token);
        if (key) {
            return new Result(true, "token已存在");
        }
        return new Result(false, "token不存在");
    }

    @RequestMapping(value = "/updateToken/{token}", method = RequestMethod.POST)
    @ResponseBody
    public Result updateToken(@PathVariable("token") String token) {
        if (redisUtil.hasKey(token)) {
            redisUtil.del(token);
            redisUtil.set(token, System.currentTimeMillis(), 60 * 30);
            return new Result(true, "更新成功");
        }
        return new Result(false, "token不存在");
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    @ApiOperation(value = "/register", httpMethod = "POST", notes = "注册")
    @ResponseBody
    public Result register(@RequestBody UserForm userForm) {
        UserBean userBean = new UserBean();
        this.setFormToBean3(userForm, userBean);
        return userService.register(userBean);
    }

    @RequestMapping(value = "/getValidCode", method = RequestMethod.POST)
    @ApiOperation(value = "/getValidCode", httpMethod = "POST", notes = "获取短信验证码")
    @ResponseBody
    public Result getValidCode(@RequestBody UserForm userForm) {
        /**
         * 获取到手机号，应该先去缓存中查询是否存在，若存在直接返回，不能短时间多次发送验证码
         * 若没有，则调用发送验证码接口
         */
        UserBean userBean = new UserBean();
        this.setFormToBean3(userForm, userBean);
        return userService.getValidCode(userBean.getTelephone());
    }

    @ApiOperation(value = "/", httpMethod = "GET", notes = "获取验证码")
    @RequestMapping(value = "/getCode", method = RequestMethod.GET)
    public void getCode(HttpServletResponse response, HttpServletRequest request) {
        try {
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setContentType("image/jpg");

            //生成随机字串
            String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
            log.info("verifyCode:{}", verifyCode);
            //存入会话session
            HttpSession session = request.getSession(true);
            session.setAttribute("_code", verifyCode.toLowerCase());
            redisUtil.set("_code", verifyCode.toLowerCase());
            //生成图片
            int w = 146, h = 33;
            VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "/login", httpMethod = "POST", notes = "登录")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public Result login(@RequestBody UserForm userForm) {
        UserBean userBean = new UserBean();
        this.setFormToBean3(userForm, userBean);
        return userService.login(userBean);
    }
}
